<?php
namespace app\common\model;

use think\Model;
use think\Db;

class ExamsPapers extends Model
{
    // 指定表名,不含前缀
    protected $name = 'exams_papers';

    //修改试卷表内容
    //返回值：true
    public function modifyExamsPapers($id,$args)
    {
        $this->where(['ep_id'=>$id])->update($args);
        return true;
    }
    /**
     * 根据参数获取列表
     * @param array $arg ['p_subject'=>2,'id'=>1....]
     * @param $field 'id,p_subject...'
     */
    public function getListByArg(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->select();
        return $arg;
    }

	 public function getListByArgOne(array $arg=null,$field='*'){
        if (!is_array($arg))return false;
        $arg = $this->where($arg)->field($field)->find();
        return $arg;
    }

    public function getListInfoByArg($arg,$field = '*',$left_join = null){
        if (empty($left_join)){
            $arg = $this->alias('a')->where($arg)->field($field)->select();
        }else{
            $table = $left_join[0];
            $condition = $left_join[1];
            $arg = $this->alias('a')->where($arg)->join($table,$condition,'left')->field($field)->select();
        }

        return $arg;
    }

    public function getExamUseNumber($identity_id,$examid,$basicid,$type=2)
    {
        if (!isset($identity_id))return false;
        $arg = $this->where(['identity_id'=>$identity_id,'ep_paperId'=>$examid,'basicid'=>$basicid,'ep_type'=>$type])->field(' count(*) AS number ')->select();
        return $arg[0]['number'];
    }

    /**
     * @param $userid 用户id
     * @param $examid 试卷id
     * @param $basicid 考场id
     * @return bool
     */
    public function getUserPaerInfo($identity_id,$examid,$basicid){
        if (!isset($userid))return false;
        $arg = $this->alias('a')->where(['a.identity_id'=>$identity_id,'a.ep_paperId'=>$examid,'a.basicid'=>$basicid])->join("papers_info b","a.ep_id = b.paperid",'left')->field('*')->select();
        if (isset($arg[0])){
            return $arg[0];
        }else{
            return false;
        }

    }
     public function exams_papers($condition,$field,$limit){
         if (!$limit){
             $limit = '0,6';
         }
       return $this->alias('e')->join('papers_info p','e.ep_infoId=p.id')->join('basic_setting s','e.basicid=s.basicid')->where($condition)->field($field)->order('p.id desc')->limit($limit)->select();
    }
}
